"use strict";
// Class definition

var messageCenter = function () {

    var datatable;
    var datatable2;

    var eventHandler = function () {
        $('.k_selectpicker').selectpicker();

        $(document).off('click', '#k_notice_tab_content .btn-search');
        $(document).on('click', '#k_notice_tab_content .btn-search', doSearch);
        $(document).off('click', '#k_string_message_tab_content .btn-search');
        $(document).on('click', '#k_string_message_tab_content .btn-search', doSearch2);

        $(document).off('show.bs.tab');
        $(document).on('show.bs.tab', 'a[data-toggle="tab"]', function (e) {
            if ($(e.target).attr('href').indexOf('string_message') !== 1) {
                initStringMessageTable();
            }
        });
    };

    /**
     * 根据后缀名获取文件图标
     * @param ext
     * @returns {string}
     */
    function getFileIcon(ext) {
        var fileIcon = 'fa-file-alt';
        if (ext === 'pdf') {
            fileIcon = 'fa-file-pdf';
        } else if (ext === 'doc' || ext === 'docx') {
            fileIcon = 'fa-file-word';
        } else if (ext === 'xls' || ext === 'xlsx') {
            fileIcon = 'fa-file-excel';
        } else if (ext === 'gif' || ext === 'jpg'
            || ext === 'jpeg' || ext === 'bmp'
            || ext === 'png') {
            fileIcon = 'fa-file-image';
        } else if (ext === 'mp3' || ext === 'ogg') {
            fileIcon = 'fa-file-audio';
        } else if (ext === 'mp4' || ext === 'webm' || ext === 'ogg') {
            fileIcon = 'fa-file-video';
        } else if (ext === 'zip' || ext === 'rar') {
            fileIcon = 'fa-file-archive';
        } else {
            fileIcon = 'fa-file-alt';
        }

        return fileIcon;
    }

    var initMessageCenterTable = function () {
        datatable = $('#notice').KDatatable({
            // datasource definition
            data: {
                type: 'remote',
                source: {
                    read: {
                        url: 'json/message-center-table.json',
                        method: 'GET'
                    }
                },
                pageSize: 10,
                serverPaging: true,
                serverFiltering: true,
                serverSorting: true
            },
            //
            // extensions: {
            //     checkbox: {},
            // },

            // layout definition
            layout: {
                scroll: false,
                footer: false
            },

            // column sorting
            sortable: false,

            pagination: true,

            translate: {
                records: {
                    processing: '数据加载中...',
                    noRecords: '未匹配到对应数据'
                },
                toolbar: {
                    pagination: {
                        items: {
                            default: {
                                first: '第一页',
                                prev: '前一页',
                                next: '下一页',
                                last: '最后一页',
                                more: '更多',
                                input: 'Page number',
                                select: '选择页面显示数'
                            },
                            info: '显示 {{start}} - {{end}} 的 {{total}}'
                        }
                    }
                }
            },

            // search: {
            //     input: $('#generalSearch'),
            // },

            // columns definition
            columns: [
                {
                    field: 'checkbox',
                    title: '序号',
                    width: 30,
                    type: 'checkbox',
                    selector: {
                        class: 'k-checkbox k-checkbox--bold k-checkbox--accent'
                    },
                    textAlign: 'center',
                    template: function (row) {
                        return row;
                    }
                },
                {
                    field: 'comefrom',
                    title: '来自',
                    textAlign: 'center',
                    template: function (row) {
                        var badge = '';
                        if (row['important_level'] === 1) {
                            badge = '<span class="k-badge k-badge--dot k-badge--notify k-badge--xl k-badge--danger"></span> ' + row[this.field];
                        } else {
                            badge = row[this.field];
                        }
                        return badge;
                    }
                },
                {
                    field: 'important_level',
                    title: '重要等级',
                    textAlign: 'center',
                    template: function (row) {
                        var badge = '';
                        if (row[this.field] === 1) {
                            badge = '<span class="badge badge-pill badge-danger k-badge--lg">重要</span>';
                        } else {
                            badge = '<span class="badge badge-pill badge-info k-badge--lg">普通</span>';
                        }
                        return badge;
                    }
                },
                {
                    field: 'abstract',
                    title: '待办摘要',
                    textAlign: 'left',
                    template: function (row) {
                        var className = '';
                        if (row['status'] === 1) {
                            className = 'text-dark'
                        } else if (row['status'] === 0) {
                            className = 'text-primary'
                        }
                        return '\
                        <span class="text-primary ' + className + '">标题：' + row[this.field].title + '</span><br>\
                        <span class="text-primary ' + className + '">内容：' + row[this.field].content + '</span><br>\
                        ';
                    }
                },
                {
                    field: 'files',
                    title: '附件',
                    template: function (row) {
                        var tag = '';
                        var url = row[this.field].url;
                        var name = row[this.field].name;
                        var iconClass = getFileIcon(row[this.field].ext);
                        var textClass = '';
                        if (row['status'] === 1) {
                            textClass = 'text-dark'
                        } else if (row['status'] === 0) {
                            textClass = 'text-primary'
                        }
                        if (row[this.field].ext === 'pdf') {
                            tag = '<a href="' + url + '" class="' + textClass + '" data-target=".pdf-view-modal" data-toggle="modal"><i class="fa ' + iconClass + ' mr-2"></i>' + name + '</a>'
                        } else {
                            tag = '<a href="' + url + '" class="' + textClass + '"><i class="fa ' + iconClass + ' mr-2"></i>' + name + '</a>';
                        }

                        return tag;
                    }
                },
                {
                    field: 'started_time',
                    title: '发起日期'
                },
                {
                    field: 'Actions',
                    title: '操作',
                    width: 100,
                    overflow: 'visible',
                    textAlign: 'left',
                    autoHide: false,
                    template: function (row) {
                        var text = '';
                        var className = '';
                        if (row['status'] === 1) {
                            text = '删除';
                            className = 'text-dark delete'
                        } else if (row['status'] === 0) {
                            text = '标记为已读';
                            className = 'text-primary mark-read'
                        }
                        return '<a href="#" class="' + className + '">' + text + '</a>';
                    }
                }]

        });
    };

    var initStringMessageTable = function () {
        datatable2 = $('#string-message').KDatatable({
            // datasource definition
            data: {
                type: 'remote',
                source: {
                    read: {
                        url: 'json/message-center-table.json',
                        method: 'GET'
                    }
                },
                pageSize: 10,
                serverPaging: true,
                serverFiltering: true,
                serverSorting: true
            },
            //
            // extensions: {
            //     checkbox: {},
            // },

            // layout definition
            layout: {
                scroll: false,
                footer: false
            },

            // column sorting
            sortable: false,

            pagination: true,

            translate: {
                records: {
                    processing: '数据加载中...',
                    noRecords: '未匹配到对应数据'
                },
                toolbar: {
                    pagination: {
                        items: {
                            default: {
                                first: '第一页',
                                prev: '前一页',
                                next: '下一页',
                                last: '最后一页',
                                more: '更多',
                                input: 'Page number',
                                select: '选择页面显示数'
                            },
                            info: '显示 {{start}} - {{end}} 的 {{total}}'
                        }
                    }
                }
            },

            // search: {
            //     input: $('#generalSearch'),
            // },

            // columns definition
            columns: [
                {
                    field: 'checkbox',
                    title: '序号',
                    width: 30,
                    type: 'checkbox',
                    selector: {
                        class: 'k-checkbox k-checkbox--bold k-checkbox--accent'
                    },
                    textAlign: 'center',
                    template: function (row) {
                        return row;
                    }
                },
                {
                    field: 'comefrom',
                    title: '来自',
                    textAlign: 'center',
                    template: function (row) {
                        var badge = '';
                        if (row['important_level'] === 1) {
                            badge = '<span class="k-badge k-badge--dot k-badge--notify k-badge--xl k-badge--danger"></span> ' + row[this.field];
                        } else {
                            badge = row[this.field];
                        }
                        return badge;
                    }
                },
                {
                    field: 'important_level',
                    title: '重要等级',
                    textAlign: 'center',
                    template: function (row) {
                        var badge = '';
                        if (row[this.field] === 1) {
                            badge = '<span class="badge badge-pill badge-danger k-badge--lg">重要</span>';
                        } else {
                            badge = '<span class="badge badge-pill badge-info k-badge--lg">普通</span>';
                        }
                        return badge;
                    }
                },
                {
                    field: 'abstract',
                    title: '待办摘要',
                    textAlign: 'left',
                    template: function (row) {
                        var className = '';
                        if (row['status'] === 1) {
                            className = 'text-dark'
                        } else if (row['status'] === 0) {
                            className = 'text-primary'
                        }
                        return '\
                        <span class="text-primary ' + className + '">标题：' + row[this.field].title + '</span><br>\
                        <span class="text-primary ' + className + '">内容：' + row[this.field].content + '</span><br>\
                        ';
                    }
                },
                {
                    field: 'files',
                    title: '附件',
                    template: function (row) {
                        var tag = '';
                        var url = row[this.field].url;
                        var name = row[this.field].name;
                        var iconClass = getFileIcon(row[this.field].ext);
                        var textClass = '';
                        if (row['status'] === 1) {
                            textClass = 'text-dark'
                        } else if (row['status'] === 0) {
                            textClass = 'text-primary'
                        }
                        if (row[this.field].ext === 'pdf') {
                            tag = '<a href="' + url + '" class="' + textClass + '" data-target=".pdf-view-modal" data-toggle="modal"><i class="fa ' + iconClass + ' mr-2"></i>' + name + '</a>'
                        } else {
                            tag = '<a href="' + url + '" class="' + textClass + '"><i class="fa ' + iconClass + ' mr-2"></i>' + name + '</a>';
                        }

                        return tag;
                    }
                },
                {
                    field: 'started_time',
                    title: '发起日期'
                },
                {
                    field: 'Actions',
                    title: '操作',
                    width: 100,
                    overflow: 'visible',
                    textAlign: 'left',
                    autoHide: false,
                    template: function (row) {
                        var text = '';
                        var className = '';
                        if (row['status'] === 1) {
                            text = '删除';
                            className = 'text-dark delete'
                        } else if (row['status'] === 0) {
                            text = '标记为已读';
                            className = 'text-primary mark-read'
                        }
                        return '<a href="#" class="' + className + '">' + text + '</a>';
                    }
                }]

        });
    };

    var doSearch = function () {
        var query = datatable.getDataSourceQuery();
        query.status = $('#status').val();
        query.important_level = $('#important_level').val();
        datatable.setDataSourceQuery(query);
        datatable.search();
    };

    var doSearch2 = function () {
        var query = datatable2.getDataSourceQuery();
        query.status = $('#status2').val();
        query.important_level = $('#important_level2').val();
        datatable2.setDataSourceQuery(query);
        datatable2.search();
    };

    return {
        // public functions
        init: function () {
            eventHandler();
            initMessageCenterTable();
        }
    };
}();

jQuery(document).ready(function () {
    messageCenter.init();
});
